package com.pansoft.kline.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.chat.activity.BaseActivity;
import com.pansoft.kline.adapter.SimpleFragmentPagerAdapter;
import com.pansoft.kline.fragment.KLineChartFragment;
import com.pansoft.kline.fragment.TimeLineChartFragment;
import com.pansoft.kline.model.IndexSelectEvent;
import com.pansoft.kline.model.KIndexSelectEvent;
import com.pansoft.kline.widget.ChartIndexPopupWindow;
import com.pansoft.kline.widget.KindexPopupWindow;
import com.pansoft.klinelib.chart.KLineView;
import com.pansoft.openplanet.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_15_MINUTE;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_3_DAY;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_DAY;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_HOUR;
import static com.pansoft.klinelib.util.KLineConstant.K_LINE_TYPE_MINUTE;


/**
 * 图标横屏界面
 * @author will
 */
public class FullScreenChartActivity extends BaseActivity {

    private TabLayout tabLayout;
    private ConstraintLayout clIndex;
    private TextView tvIndex;
    private ImageView ivIndexTopRight, ivIndexBottomRight;

    private String marketId;
    private ViewPager viewPager;
    private ImageView ivRefresh;
//    private ObjectAnimator refreshAnim;

    /** 加载各指标图表**/
    private List<Fragment> fragments = new ArrayList<>();
    private SimpleFragmentPagerAdapter simpleFragmentPagerAdapter;

    public static void launch(Context context, int index, String marketId) {
        Intent intent = new Intent(context, FullScreenChartActivity.class);
        intent.putExtra("index", index);
        intent.putExtra("marketId", marketId);
        context.startActivity(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.open_planet_activity_fullscreen_chart);
        tabLayout = (TabLayout) findViewById(R.id.tab);
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        marketId = getIntent().getStringExtra("marketId");
        Fragment[] fragmentArray = {TimeLineChartFragment.newInstance(K_LINE_TYPE_MINUTE,marketId), KLineChartFragment.newInstance(K_LINE_TYPE_15_MINUTE,marketId),
                KLineChartFragment.newInstance(K_LINE_TYPE_HOUR, marketId), KLineChartFragment.newInstance(K_LINE_TYPE_DAY, marketId),
                KLineChartFragment.newInstance(K_LINE_TYPE_3_DAY, marketId)};
        fragments.addAll(Arrays.asList(fragmentArray));
        String[] titleArray = getResources().getStringArray(R.array.array_chart_k_index);
        List<String> titles = Arrays.asList(titleArray);
//        viewPager.setOffscreenPageLimit(fragments.length);
        simpleFragmentPagerAdapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(simpleFragmentPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        ivRefresh = (ImageView) findViewById(R.id.iv_refresh);
        ivRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                refreshAnim.start();
                refreshKChart();
            }
        });
//        refreshAnim = ObjectAnimator.ofFloat(ivRefresh, "rotation", 0, 360);
//        refreshAnim.setDuration(2000);
//        refreshAnim.setRepeatMode(ValueAnimator.RESTART);
//        refreshAnim.setRepeatCount(0);
        viewPager.setCurrentItem(getIntent().getIntExtra("index", 0));
        initKIndexView();
        initIndexView();
    }

    private void initKIndexView() {
        final View tabItemView = LayoutInflater.from(this).inflate(R.layout.chart_layout_tab_item, null, false);
        tabLayout.getTabAt(4).setCustomView(tabItemView);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 4) {
                    KindexPopupWindow.setKIndexTabSelected(FullScreenChartActivity.this, tabLayout);
                } else {
                    KindexPopupWindow.setKIndexTabUnSelected(FullScreenChartActivity.this, tabLayout);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                if (tab.getPosition() == 4) {
                    KindexPopupWindow.setKIndexTabShowPop(FullScreenChartActivity.this, tabLayout);
                    new KindexPopupWindow(FullScreenChartActivity.this,
                            FullScreenChartActivity.this.getWindow().getDecorView(), tabLayout, ((TextView)tab.getCustomView().findViewById(R.id.tv_K_index)).getText().toString());
                }
            }
        });
    }

    /**
     *1分K，日K等指标选择
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSolveKIndexSelectedEvent(KIndexSelectEvent event) {
        KindexPopupWindow.setTabIndexText(FullScreenChartActivity.this, tabLayout, event.getIndexName());
        ((KLineChartFragment)fragments.get(4)).reloadData(event.getIndex());

    }

    /**
     *MA， KDJ等指标选择
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onIndexSelectedEvent(IndexSelectEvent event) {
        switch (event.getIndex()) {
            case "MA":
                KLineView.CHART_MAIN_INDEX = "MA";
                if (viewPager.getCurrentItem() == 0) {
                    ((TimeLineChartFragment) fragments.get(viewPager.getCurrentItem())).showMa();
                } else {
                    ((KLineChartFragment) fragments.get(viewPager.getCurrentItem())).showMa();
                }
                break;
            case "BOLL":
                KLineView.CHART_MAIN_INDEX = "BOLL";
                if (viewPager.getCurrentItem() == 0) {
                    ((TimeLineChartFragment) fragments.get(viewPager.getCurrentItem())).showBoll();
                } else {
                    ((KLineChartFragment) fragments.get(viewPager.getCurrentItem())).showBoll();
                }
                break;
            case "MACD":
                KLineView.CHART_SUB_INDEX = "MACD";
                if (viewPager.getCurrentItem() == 0) {
                    ((TimeLineChartFragment) fragments.get(viewPager.getCurrentItem())).showMacd();
                } else {
                    ((KLineChartFragment) fragments.get(viewPager.getCurrentItem())).showMacd();
                }
                break;
            case "KDJ":
                KLineView.CHART_SUB_INDEX = "KDJ";
                if (viewPager.getCurrentItem() == 0) {
                    ((TimeLineChartFragment) fragments.get(viewPager.getCurrentItem())).showKdj();
                } else {
                    ((KLineChartFragment) fragments.get(viewPager.getCurrentItem())).showKdj();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initIndexView() {
        clIndex = (ConstraintLayout) findViewById(R.id.cl_index);
        tvIndex = (TextView) findViewById(R.id.tv_index);
        ivIndexTopRight = (ImageView) findViewById(R.id.iv_full_right_top_triangle);
        ivIndexBottomRight = (ImageView) findViewById(R.id.iv_full_right_bottom_triangle);

        clIndex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ivIndexTopRight.setVisibility(View.VISIBLE);
                ivIndexBottomRight.setVisibility(View.INVISIBLE);
                new ChartIndexPopupWindow(FullScreenChartActivity.this,
                        FullScreenChartActivity.this.getWindow().getDecorView(), clIndex);
            }
        });
    }

    private void refreshKChart() {
        if (viewPager.getCurrentItem() == 0) {
            ((TimeLineChartFragment) fragments.get(viewPager.getCurrentItem())).reloadData();
            ((KLineChartFragment) fragments.get(viewPager.getCurrentItem() + 1)).reloadData("");
        } else if (viewPager.getCurrentItem() == 1){
            ((TimeLineChartFragment) fragments.get(viewPager.getCurrentItem() - 1)).reloadData();
            ((KLineChartFragment) fragments.get(viewPager.getCurrentItem())).reloadData("");
            ((KLineChartFragment) fragments.get(viewPager.getCurrentItem() + 1)).reloadData("");
        } else if (viewPager.getCurrentItem() == fragments.size() - 1) {
            ((KLineChartFragment) fragments.get(viewPager.getCurrentItem() - 1)).reloadData("");
            ((KLineChartFragment) fragments.get(viewPager.getCurrentItem())).reloadData("");
        } else {
            ((KLineChartFragment) fragments.get(viewPager.getCurrentItem() - 1)).reloadData("");
            ((KLineChartFragment) fragments.get(viewPager.getCurrentItem())).reloadData("");
            ((KLineChartFragment) fragments.get(viewPager.getCurrentItem() + 1)).reloadData("");
        }
    }
}
